package gui;

import impl.ClientProxyImpl;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Main GUI frame which combines a message display to view messages
 * and a textarea to write messages.
 * Implements methods to communicate with impl-classes.
 *
 * @author Dominik Korittki
 * @author Steffen Grushka
 * @version 0.1
 */
public class ChatGUI extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private TextPanel textPanel;
	private WritePanel writePanel;
	private ClientProxyImpl clientproxy;

	private JPanel contentPane;

	

	/**
	 * Create the frame.
	 */
	public ChatGUI(ClientProxyImpl clientproxy) {
		this.clientproxy = clientproxy;
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0};
		gbl_contentPane.rowHeights = new int[]{200, 30, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		textPanel = new TextPanel();
		GridBagConstraints gbc_textWindow = new GridBagConstraints();
		gbc_textWindow.insets = new Insets(0, 0, 5, 0);
		gbc_textWindow.fill = GridBagConstraints.BOTH;
		gbc_textWindow.gridx = 0;
		gbc_textWindow.gridy = 0;
		contentPane.add(textPanel, gbc_textWindow);
		
		writePanel = new WritePanel();
		writePanel.setBorder(null);
		GridBagConstraints gbc_writePanel = new GridBagConstraints();
		gbc_writePanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_writePanel.gridx = 0;
		gbc_writePanel.gridy = 1;
		contentPane.add(writePanel, gbc_writePanel);		
		
		//create actionlistener
		writePanel.getBtnSenden().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ChatGUI.this.clientproxy.sendMessage(writePanel.getTextField().getText());
				writePanel.getTextField().setText("");
			}
		});
		
		writePanel.getTextField().addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {}
			
			@Override
			public void keyReleased(KeyEvent arg0) {}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER) {
					ChatGUI.this.clientproxy.sendMessage(writePanel.getTextField().getText());
					writePanel.getTextField().setText("");
				}
					
					
			}
		});
		
		addWindowListener(new WindowAdapter() { 
	         public void windowClosing(WindowEvent e) { 
	        	 System.out.println("Logout from Server and exit.");
	        	 if(ChatGUI.this.clientproxy != null)
	        		 ChatGUI.this.clientproxy.logout();
	        	 dispose();
	        	 System.exit(0);
	         } 
	    });
	}
	
	/**
	 * Pop up dialog which provides a textfield for entering a username
	 * @return username name which the user has chosen
	 */
	public String usernameRequest() {
		return JOptionPane.showInputDialog("Enter username:");
	}
	
	/**
	 * Pop up dialog which provides a textfield for entering a username
	 * @return username name which the user has chosen
	 */
	public String ipRequest() {
		return JOptionPane.showInputDialog("Enter server IP:");
	}
	
	/**
	 * Append message to the message display
	 * @param message String to append to the message display
	 */
	public void appendMessage(String message) {
		textPanel.addText(message);
	}

}
